package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.constants.admin.AdminConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult insert(WmUser wmUser) {
        //1.检查参数

        if (wmUser==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }
        //2.存储

        save(wmUser);

        //3.返回
        return ResponseResult.okResult(wmUser);
    }

    @Override
    public ResponseResult findByName(String name) {

        //1.检查参数

        if (name==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }

        //2.查询信息

        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(WmUser::getName, name);

        WmUser wmUser = getOne(queryWrapper);

        //3.返回信息


        return ResponseResult.okResult(wmUser);
    }

    @Override
    public ResponseResult login(WmUserDto dto) {

        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数异常");
        }

        if (dto.getName() != null && dto.getPassword() != null) {

            LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper();

            queryWrapper.eq(WmUser::getName, dto.getName())
                    .eq(WmUser::getStatus, AdminConstants.PASS_AUTH.intValue());

            List<WmUser> wmUserList = list(queryWrapper);

            for (WmUser wmUser : wmUserList) {
                String md5Password = DigestUtils.md5DigestAsHex((dto.getPassword() + wmUser.getSalt()).getBytes());

                if (md5Password.equals(wmUser.getPassword())) {

                    Map map = new HashMap();
                    map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));
                    wmUser.setSalt("");
                    wmUser.setPassword("");
                    map.put("user", wmUser);
                    return ResponseResult.okResult(map);
                }
            }
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "请填写用户名和密码");
    }
}
